package com.jet.chat.client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import com.jet.chat.exception.ChatExceptionMessageRead;
import com.jet.chat.service.ChatMessage;
import com.jet.chat.service.ChatMessageService;


public class ChatClientConsoleReader implements Runnable {
	private PrintWriter writer;
	
	public ChatClientConsoleReader(PrintWriter writer) {
		this.writer = writer;
	}

	@Override
	public void run() {
		BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
		ChatMessage chatMessage;
		try {
			while ((chatMessage = new ChatMessage(consoleReader)) != null) {
				if (chatMessage.getMessage().equals("/q")) {
					System.exit(0);
				} else {
					ChatMessageService.send(chatMessage, writer);
				}
			}
		} catch (ChatExceptionMessageRead e) {
			e.printStackTrace();
		}
	}

}
